package com.wisdomparking.tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 页面类
 *
 * @author fanghui
 */
public class PageForAjax {

    private int pageNo;    // 当前页
    private int pageSize;  // 每页记录条数
    private int maxPage;   // 最大的页码数
    private int count;     // 总记录条数
    private String query;  // 参数字符串
    private String anchor; // 锚点

    private boolean isQuery = false;

    /**
     * 初始化页面类
     *
     * @param pageNo 当前页
     * @param pageSize 每页记录条数
     * @param count 总记录条数
     * @param uri URL路径
     * @param query 参数字符串
     */
    public PageForAjax(int pageNo, int pageSize, int count, String uri, String query) {

        setPageNo(pageNo);
        this.pageSize = pageSize;

        if (null == query) {
            isQuery = true;
            this.query = uri;
        } else {
            this.query = uri + "?" + query;
        }

        setCount(count);
    }

    /**
     * 初始化页面类
     *
     * @param pageNo 当前页
     * @param pageSize 每页记录条数
     * @param count 总记录条数
     * @param uri URL路径
     * @param query 参数字符串
     * @param anchor 锚点
     */
    public PageForAjax(int pageNo, int pageSize, int count, String uri, String query, String anchor) {

        setPageNo(pageNo);
        this.pageSize = pageSize;

        if (null == query) {
            isQuery = true;
            this.query = uri;
        } else {
            this.query = uri + "?" + query;
        }

        this.anchor = anchor;

        setCount(count);
    }

    public int getPageNo() {
        return (pageNo > maxPage) ? maxPage : pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = (pageNo < 1) ? 1 : pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getMaxPage() {
        return maxPage;
    }

    public void setMaxPage(int maxPage) {
        this.maxPage = maxPage;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        maxPage = (count % pageSize) == 0 ? (count / pageSize) : (count
                / pageSize + 1);
        if (0 == maxPage) {
            maxPage = 1;
        }
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public void setAnchor(String anchor) {
        this.anchor = anchor;
    }

    public String getAnchor() {
        return anchor;
    }


    private static final String REG_PAGE_NUM = "page_num=([^&]+|)";

    private static final String PLACEHOLDER = "{}";
    private static final String PLACEHOLDER_1 = "{1}";


    private static final String UP_HTML = "<a href=\"javascript:void(0);\" onclick=\"changePage('" + PLACEHOLDER + "');\" class=\"prev\">上一页</a>&nbsp;";
    private static final String UP_HTML_DISABLED = "<a style=\"cursor:hand\" class=\"prev\">上一页</a>&nbsp;";

    private static final String NEXT_HTML = "<a href=\"javascript:void(0);\" onclick=\"changePage('" + PLACEHOLDER + "');\" class=\"next\">下一页</a>";
    private static final String NEXT_HTML_DISABLED = "<a style=\"cursor:hand\" class=\"next\">下一页</a>";

    private static final String CURRENT_HTML = "<a class=\"on1\">{}</a>&nbsp;";
    private static final String GENERAL_HTML = "<a href=\"javascript:void(0);\" onclick=\"changePage('" + PLACEHOLDER + "');\">{1}</a>&nbsp;";

    private static final String SYMBOL_ELLIPSIS = " … ";
    private static final String STR_PAGE_NUM = "page_num=";
    private static final String STR_PAGE_COUNT = "page_count=";

    @Override
    public String toString() {
        // 检查query是否包含page_num, page_count
        if (isQuery) {
            query += "?page_num=1&page_count=" + pageSize;
        } else {
            if (!isFind(STR_PAGE_NUM, query)) {
                query += "&page_num=1";
            }
            if (!isFind(STR_PAGE_COUNT, query)) {
                query += ("&page_count=" + pageSize);
            }
        }

        int currNo = getPageNo();

        // 拼装字符串
        // 第一页
        StringBuffer queryStr = new StringBuffer();

        if (currNo == 1) {
            queryStr.append(UP_HTML_DISABLED);
            queryStr.append(CURRENT_HTML.replace(PLACEHOLDER, "1"));

        } else {
            queryStr.append(UP_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + (currNo - 1))));
            queryStr.append(GENERAL_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, "page_num=1")).replace(PLACEHOLDER_1, "1"));

        }

        // 中间页码
        if (maxPage <= 7) {
            for (int i = 2; i < maxPage; i++) {
                if (i == currNo) {
                    queryStr.append(CURRENT_HTML.replace(PLACEHOLDER, String.valueOf(i)));
                } else {
                    queryStr.append(GENERAL_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + i)).replace(PLACEHOLDER_1, String.valueOf(i)));
                }
            }
        } else {
            int n = 0;

            // 控制当前页以前
            int i = (currNo - 2);

            if (i > 2) {
                queryStr.append(SYMBOL_ELLIPSIS);
            }

            if (i <= 1) {
                n++;
            } else {
                queryStr.append(GENERAL_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + i)).replace(PLACEHOLDER_1, String.valueOf(i)));
            }

            int j = (currNo - 1);

            if (j <= 1) {
                n++;
            } else {
                queryStr.append(GENERAL_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + j)).replace(PLACEHOLDER_1, String.valueOf(j)));
            }

            // 当前页
            if (currNo != 1 && currNo != maxPage) {
                queryStr.append(CURRENT_HTML.replace(PLACEHOLDER, String.valueOf(currNo)));
            }

            // 控制当前页以后
            int k = 2 + n;
            int g = currNo + 1;
            for (int l = 0; l < k; l++, g++) {
                if (maxPage <= g) {
                    break;
                }
                queryStr.append(GENERAL_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + g)).replace(PLACEHOLDER_1, String.valueOf(g)));
            }
            if ((maxPage - currNo) > 3) {
                queryStr.append(SYMBOL_ELLIPSIS);
            }
        }

        // 最后一页
        if (maxPage == 1) {
            queryStr.append(NEXT_HTML_DISABLED);

        } else if (currNo == maxPage) {
            queryStr.append(CURRENT_HTML.replace(PLACEHOLDER, String.valueOf(maxPage)));
            queryStr.append(NEXT_HTML_DISABLED);

        } else {
            queryStr.append(GENERAL_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + String.valueOf(maxPage))).replace(PLACEHOLDER_1, String.valueOf(maxPage)));
            queryStr.append(NEXT_HTML.replace(PLACEHOLDER, query.replaceFirst(REG_PAGE_NUM, STR_PAGE_NUM + (currNo + 1))));

        }

        return queryStr.toString().replace("?&", "?");
    }

    /**
     * 检查是否存在
     *
     * @param regex 正则表达式
     * @param value 字符串
     * @return boolean
     */
    public static boolean isFind(String regex, String value) {

        Matcher matcher = Pattern.compile(regex).matcher(value);

        return matcher.find();
    }

}